/****************************************************
	文件：MD5Util.cs
	作者：HuskyT
	邮箱：1005240602@qq.com
	日期：2021/1/24 9:26:52
	功能：MD5码 生成工具
*****************************************************/

using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;
using UnityEngine;

namespace HTFW.Utility.Global
{
    public class MD5Util : Singleton<MD5Util>
    {
        /// <summary>
        /// 储存Md5码，filePath为文件路径，md5SavePath为储存md5码路径
        /// </summary>
        /// <param name="filePath"></param>
        /// <param name="md5SavePath"></param>
        public void SaveMd5(string filePath, string md5SavePath)
        {
            string md5 = BuildFileMd5(filePath);
            string name = filePath + "_md5.dat";
            if (File.Exists(name))
            {
                File.Delete(name);
            }

            StreamWriter sw = new StreamWriter(name, false, Encoding.UTF8);
            if (sw != null)
            {
                sw.Write(md5);
                sw.Flush();
                sw.Close();
            }
        }

        /// <summary>
        /// 储存Md5码，filePath为文件路径
        /// </summary>
        /// <param name="filePath"></param>
        public void SaveMd5(string filePath)
        {
            string md5 = BuildFileMd5(filePath);
            string name = filePath + "_md5.dat";
            if (File.Exists(name))
            {
                File.Delete(name);
            }

            StreamWriter sw = new StreamWriter(name, false, Encoding.UTF8);
            if (sw != null)
            {
                sw.Write(md5);
                sw.Flush();
                sw.Close();
            }
        }

        /// <summary>
        /// 获取之前储存的Md5码
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public string GetMd5(string path)
        {
            string name = path + "_md5.dat";
            try
            {
                StreamReader sr = new StreamReader(name, Encoding.UTF8);
                string content = sr.ReadToEnd();
                sr.Close();
                return content;
            }
            catch
            {
                return "";
            }
        }

        /// <summary>
        /// 生成MD5码
        /// </summary>
        /// <param name="fliePath"></param>
        /// <returns></returns>
        public string BuildFileMd5(string fliePath)
        {
            string filemd5 = null;
            try
            {
                using (var fileStream = File.OpenRead(fliePath))
                {
                    var md5 = MD5.Create();
                    var fileMD5Bytes =
                        md5.ComputeHash(fileStream); //计算指定Stream 对象的哈希值                                     
                    filemd5 = FormatMD5(fileMD5Bytes);
                }
            }
            catch (Exception e)
            {
                Debug.LogError(e);
            }

            return filemd5;
        }

        /// <summary>
        /// 将byte[]转换成字符串
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        string FormatMD5(Byte[] data)
        {
            return BitConverter.ToString(data).Replace("-", "").ToLower();
        }
    }
}